var express = require('express');
var router = express.Router();
var { model406 } = require('../model/model406')
/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});
// model406.create({})
router.post('/add', async (req, res, next) => {
  model406.create(req.body)
  res.send('添加成功')
})
router.post('/list', async (req, res, next) => {
  let list = await model406.find()
  res.send({ listAll: list })
})
router.post('/update', async (req, res, next) => {
  await model406.updateOne({ _id: req.body._id }, req.body)
  console.log("🚀 ~ router.post ~ req.body:", req.body)
  res.send('更新成功')
})
// 批量更新
router.post('/all', async (req, res, next) => {
  await model406.updateMany({ ischeck: true }) 
  res.send('全选成功')
})
// 删除
router.post('/delete', async (req, res, next) => {
  await model406.deleteOne({ _id: req.body._id })
  res.send('删除成功')
})
//
router.post('/delall', async (req, res, next) => {
  req.body.forEach(async (item) => {
    console.log("🚀 ~ req.body.forEach ~ item:", item)
    // await model406.deleteOne({ _id: item._id })
  })
  res.send('批量删除成功')
})


// 条件查询 
router.post('/search', async (req, res, next) => {
  let { context } = req.body
  let arr = []
  if (context) {
    arr.push({ context: { $regex: context } })
    let list = await listModel.find({ $and: arr })
    res.send(list)
  }
})


let Multipatry = require('multiparty')
// 上传文件
router.post('/upload', async (req, res, next) => {
  let form = new Multipatry.Form()
  form.uploadDir = 'uploads'
  form.parse(req, (err, fields, files) => {
    console.log("🚀 ~ form.parse ~ files:", files)
    console.log("🚀 ~ form.parse-------:", files.null[0].path)
    res.send({
      status: 'success',
      url: 'http://localhost:4002/' + files.null[0].path
    })
  })
})

module.exports = router;
